package com.qf.web.controller;

import com.qf.domain.Cart;
import com.qf.domain.Goods;
import com.qf.domain.User;
import com.qf.service.CartService;
import com.qf.service.GoodsService;
import com.qf.service.impl.CartServiceImpl;
import com.qf.service.impl.GoodsServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.zip.Inflater;

/**
 * projectName:myxiaomi
 * author:jww
 * time:2024/4/17 10:56
 * description:
 */
@WebServlet("/cartservlet")
public class CartServlet extends BaseServlet {
    /**
     * 添加购物车
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    //http://localhost:8080/cartservlet?method=addCart&goodsId=1&number=1
    public String addCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        //2获取参数
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        //3校验
        if (StringUtils.isEmpty(goodsId)) {
            request.setAttribute("msg", "商品ID不能为空");
            return "/message.jsp";
        }
        int num = 1;
        if (!StringUtils.isEmpty(number)) {
            try {
                num = Integer.parseInt(number);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        //4调用业务逻辑
        try {
            CartService cartService = new CartServiceImpl();
            int good_id = Integer.parseInt(goodsId);
            //查询商品信息
            GoodsService goodsService = new GoodsServiceImpl();
            Goods goods = goodsService.findById(good_id);
            //查看购物车是否存在这个商品
            Cart cart = cartService.find(user.getId(), good_id);
            if (cart != null) {//更新操作
                cart.setNum(cart.getNum() + num);//1+1=2
                cart.setMoney(goods.getPrice().multiply(new BigDecimal(cart.getNum())));
                cartService.modify(cart);
            } else {//新增购物车
                cart = new Cart(user.getId(), good_id, num, goods.getPrice().multiply(new BigDecimal(num)));
                //添加购物车
                cartService.add(cart);
            }
            return "redirect:/cartSuccess.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "添加购物车失败" + e.getMessage());
            return "/message.jsp";
        }
    }

    /**
     * 查看购物车功能
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    //http://localhost:8080/cartservlet?method=getCart
    public String getCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        //2调用业务逻辑
        try {
            CartService cartService = new CartServiceImpl();
            List<Cart> cartList = cartService.find(user.getId());
            //转发重定向
            request.setAttribute("cartList", cartList);
            return "/cart.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查看购物车失败" + e.getMessage());
            return "/message.jsp";
        }
    }

    /**
     * AJAX操作购物车
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    //cartservlet?method=addCartAjax&goodsId="+pid+"&number=1
    public String addCartAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        //2获取参数
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        //3非空验证
        if (StringUtils.isEmpty(goodsId)) {
            request.setAttribute("msg", "商品ID不能为空");
            return "/message.jsp";
        }
        try {
            //4调用业务逻辑
            int goods_id = Integer.parseInt(goodsId);
            int num = Integer.parseInt(number);
            CartService cartService = new CartServiceImpl();
            if (num == 0) {//删除商品
                cartService.remove(user.getId(), goods_id);
            } else if (num == 1 || num == -1) {//更新功能
                //查找商品
                Cart cart = cartService.find(user.getId(), goods_id);
                if (cart != null) {
                    GoodsService goodsService = new GoodsServiceImpl();
                    Goods goods = goodsService.findById(goods_id);
                    cart.setNum(cart.getNum() + num);
                    cart.setMoney(goods.getPrice().multiply(new BigDecimal(cart.getNum())));
                    cartService.modify(cart);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "更新购物车失败" + e.getMessage());
            return "/message.jsp";
        }
        return null;
    }

    /**
     * 清空购物车功能
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    //cartservlet?method=clearCartAjax
    public String clearCartAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        try {
            //2调用业务逻辑
            CartService cartService=new CartServiceImpl();
            cartService.clear(user.getId());
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "清空购物车失败" + e.getMessage());
            return "/message.jsp";
        }
        return null;
    }
}


